package cn.com.huzo.repository.master.impl;

import cn.com.huzo.model.entities.master.ColourCatalogueEntity;
import huzofw.repository.impl.GenericRepositoryImpl;
import huzofw.repository.impl.MyBatisDaoUtils;
import cn.com.huzo.repository.master.ColourCatalogueRepository;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Repository;

/**
 * 颜色目录数据访问对象。
 *
 * @author 吴旭
 */
@Repository("ColourCatalogueRepository")
public class ColourCatalogueRepositoryImpl extends GenericRepositoryImpl<ColourCatalogueEntity, Long>
        implements ColourCatalogueRepository {

    private final String namespace = "cn.com.huzo.repository.master.ColourCatalogueRepository";

    /**
     * 构造函数,设置the instance to ColourCatalogueRepositoryImpl.class.
     */
    public ColourCatalogueRepositoryImpl() {
        super(ColourCatalogueEntity.class);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ColourCatalogueEntity fetchByKey(Long key) {
        return getSqlSession().selectOne(MyBatisDaoUtils.getMapperQueryId(
                namespace, "fetchByKey"), key);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Long insert(ColourCatalogueEntity instance) {
        getSqlSession().insert(MyBatisDaoUtils.getMapperQueryId(
                namespace, "insert"), instance);

        // 返回出入后的记录ID
        return instance.getCatalogueId();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Integer update(ColourCatalogueEntity instance) {
        return getSqlSession().update(MyBatisDaoUtils.getMapperQueryId(
                namespace, "update"), instance);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Integer changeParentCatalogue(Map<String, Object> nodeParams) {
        return getSqlSession().update(MyBatisDaoUtils.getMapperQueryId(
                namespace, "changeParentCatalogue"), nodeParams);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Integer delete(Long key) {
        return getSqlSession().delete(MyBatisDaoUtils.getMapperQueryId(
                namespace, "delete"), key);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Integer isNullNode(Long key) {
        return getSqlSession().selectOne(MyBatisDaoUtils.getMapperQueryId(
                namespace, "isNullNode"), key);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List<ColourCatalogueEntity> fetchTreeNodeByParentKey(String rootNodeName, Long parentKey) {
        Map<String, Object> criterion = new HashMap<>();
        criterion.put("RootNodeName", rootNodeName);
        criterion.put("ParentKey", parentKey);

        return getSqlSession().selectList(MyBatisDaoUtils.getMapperQueryId(
                namespace, "fetchTreeNodeByParentKey"), criterion);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String fetchNewOrderNo(Long parentId) {
        return getSqlSession().selectOne(MyBatisDaoUtils.getMapperQueryId(
                namespace, "fetchNewOrderNo"), parentId);
    }
}
